
//
// 130424 - AH - Created.
//

package org.japril.swing.jutil.xmloperations;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.japril.jutil.xmloperations.XmlOperation;
import org.japril.jutil.xmloperations.XmlOperations;
import org.japril.swing.PopupListener;
import org.japril.swing.TestFrame;

/**
 * <p>
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class XmlOperationsPanel extends JPanel 
{
  private XmlOperationsTableModel m_model=new XmlOperationsTableModel();
  
  private final JTable m_table=new JTable(m_model);
  
  private final XmlOperationPathsPanel m_paths=new XmlOperationPathsPanel();
  
  public XmlOperationPathsPanel getPathsPanel(){ return m_paths; }
 
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public XmlOperationsPanel(){ this(null); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param ops {@link XmlOperations} to view, if {@code null} creates.
   */
  public XmlOperationsPanel(XmlOperations ops)
  {
    setLayout(new BorderLayout());
    
    JPanel north=new JPanel(new FlowLayout(FlowLayout.CENTER));
    north.add(new JLabel("Operations"));
    add(north, BorderLayout.NORTH);
    
    JSplitPane sp=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    sp.setDividerLocation(250);
    JScrollPane scp=new JScrollPane(m_table);
    sp.setLeftComponent(scp);
    sp.setRightComponent(m_paths);
    add(sp, BorderLayout.CENTER);
    
    JPopupMenu pm=new JPopupMenu();
    pm.add(m_createOperation);
    pm.add(m_removeOperation);
    pm.add(m_desc);
    new PopupListener(pm, scp);
    new PopupListener(pm, m_table);
    
    m_table.getSelectionModel().addListSelectionListener(
      new ListSelectionListener()
      {
        public void valueChanged(ListSelectionEvent e)
        {
          int sidx=m_table.getSelectedRow();
          if (sidx!=-1)
          {
            XmlOperation o=m_model.getOperation(sidx);
            System.out.println("Oper: "+o);
            m_paths.setOperation(o);
          }
        }
      }
    );
    
    setOperations(ops);
  }
  
  /**
   * <p>
   *   Sets
   *   {@link XmlOperations} to view.
   * </p>
   * @param ops {@link XmlOperations}, if {@code null} creates empty.
   */
  public void setOperations(XmlOperations ops)
  {
    m_model=new XmlOperationsTableModel(ops);
    m_table.setModel(m_model);
  }
  
  /**
   * <p>
   *   Gets
   *   {@link XmlOperations} viewed.
   * </p>
   * @return {@link XmlOperations}.
   */
  public XmlOperations getOperations(){ return m_model.getOperations(); }
  
  public final Action m_createOperation=new AbstractAction("Create Operation")
  {
    public void actionPerformed(ActionEvent e)
    {
      String name=JOptionPane.showInputDialog("Name");
      name=(name==null) ? "" : name.trim();
      if (name.length()>0)
      {
        m_model.createProto(name); 
      }
    }
  };
  
  public final Action m_removeOperation=new AbstractAction("Remove Operation")
  {
    public void actionPerformed(ActionEvent e) 
    {
      int selected=m_table.getSelectedRow();
      if (selected!=-1) m_model.remove(selected);
    }
  };
  
  private XmlOperationDescDialog m_descDlg=null;
  
  public final Action m_desc=new AbstractAction("Desc")
  {
    public void actionPerformed(ActionEvent e)
    {
      int selected=m_table.getSelectedRow();
      if (selected!=-1) 
      {
        if (m_descDlg==null) m_descDlg=new XmlOperationDescDialog();
        XmlOperation o=m_model.getOperation(selected);
        m_descDlg.showDialog(o);
      }
    }
  };
  
  public static void main(String[] args)
  {
    TestFrame.show(new XmlOperationsPanel());  
  }
  
}
